/*
 * Copyright [2013-2021], Alibaba Group Holding Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.polardbx.optimizer.memory;

import com.alibaba.polardbx.common.properties.MppConfig;
import com.alibaba.polardbx.optimizer.core.row.Row;

/**
 * Encapsulating the logic of applying memory which need align to the 0.5Mb.
 */
public interface MemoryAllocatorCtx {

    long BLOCK_SIZE = MppConfig.getInstance().getBlockSize();

    default void allocateReservedMemory(Row row) {
        allocateReservedMemory(row.estimateSize());
    }

    /**
     * allocate the memory which can't be spill.
     */
    void allocateReservedMemory(long bytes);

    boolean tryAllocateReservedMemory(long bytes);

    boolean tryAllocateRevocableMemory(long bytes);

    /**
     * allocate the memory which can be spill.
     */
    void allocateRevocableMemory(long bytes);

    void releaseReservedMemory(long bytes, boolean immediately);

    void releaseRevocableMemory(long bytes, boolean immediately);

    long getReservedAllocated();

    long getRevocableAllocated();

    long getAllAllocated();

    String getName();
}
